package com.shoujifeng.snowmusic.player.push;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.baidu.android.pushservice.CustomPushNotificationBuilder;
import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.shoujifeng.snowmusic.db.DatabaseUtil;
import com.shoujifeng.snowmusic.entity.Mp3Info;
import com.shoujifeng.snowmusic.player.MyTabHost;
import com.shoujifeng.snowmusic.player.R;
import com.shoujifeng.snowmusic.player.StartActivity;
import com.shoujifeng.snowmusic.player.setActivity;
import com.shoujifeng.snowmusic.player.http.DownloadManager;
import com.shoujifeng.snowmusic.player.http.GlobalValue;
import com.shoujifeng.snowmusic.player.http.ServerAccess;
import com.shoujifeng.snowmusic.player.utils.MediaUtil;

/*
 * 云推送Demo主Activity。
 * 代码中，注释以Push标注开头的，表示接下来的代码块是Push接口调用示例
 */
public class PushDemoActivity extends Activity implements View.OnClickListener {
    
	private MediaUtil mediaUtil;
	private List<Mp3Info> mp3Infos;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.start_layout);

		if(GlobalValue.g_DownloadManager == null)
			GlobalValue.g_DownloadManager = new DownloadManager(this);
		
		GlobalValue.g_DownloadManager.readDownload();
		
		start(); 
		
		try {
 			SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(PushDemoActivity.this);
			String value = spf.getString(ServerAccess.PUSH, "yes");
			if (value.equals("yes")) {
				PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY, "Ps5waCpfnukKGUFfgxEN1w9N");
			}else {
				PushManager.stopWork(getApplicationContext());
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

	private void start() {
		new AsyncTask<Integer, Integer, Integer> ()
		{

			@Override
			protected Integer doInBackground(Integer... params) {
				
				try {
					
					DatabaseUtil  databaseUtil = new DatabaseUtil(PushDemoActivity.this);
					databaseUtil.clearMusicList();
					mediaUtil  = new MediaUtil();
				    mp3Infos = mediaUtil.getMp3Infos(PushDemoActivity.this);
					if(mp3Infos.size()!=0){
						for(int i = 0;i<mp3Infos.size();i++){
							String musicName  =  mp3Infos.get(i).getTitle().toString();
							String playerName =  mp3Infos.get(i).getArtist().toString();
							long duration     =  mp3Infos.get(i).getDuration();
							long size         =  mp3Infos.get(i).getSize();
							String url        =  mp3Infos.get(i).getUrl();
							String album      =  mp3Infos.get(i).getAlbum();
							
							databaseUtil.insertMusic(musicName, playerName, duration, size, url,album);
						}
					} else {
						Thread.sleep(2000);
					}
					//Thread.sleep(2000);
				} catch (Exception e) {
					// TODO: handle exception
				}
				
				publishProgress(0);
				return null;
			}

			@Override
			protected void onProgressUpdate(Integer... values) {
				
				Intent intent = new Intent(PushDemoActivity.this, MyTabHost.class);
				startActivity(intent);
				finish();
				
				super.onProgressUpdate(values);
			}
			
			
		}.execute(0);
	}
}
